//
//  WYStatusToolBar.swift
//  WYWeibo
//
//  Created by wangyi on 16/7/14.
//  Copyright © 2016年 wangyi. All rights reserved.
//

import UIKit
import SnapKit

//微博的 toolbar
class WYStatusToolBar: UIView {

  //MARK -- 懒加载
    private lazy var resweetButton: UIButton = self.addChildButton("timeline_icon_retweet", title: "转发")
    private lazy var commentButton: UIButton = self.addChildButton("timeline_icon_comment",title: "评论")
    private lazy var unlikeButton: UIButton = self.addChildButton("timeline_icon_unlike",title: "赞")
    
    //数据模型
    var statusViewModel: WYStatusViewModel? {
        //赋值
        didSet {
            //值得传递过程 是从模型 WYStatus ->  WYStatusViewModel -> 控制器
            
//            let retweetCount = statusViewModel?.status?.reposts_count
//            let commentCount = statusViewModel?.status?.comments_count
//            let unlikCount = statusViewModel?.status?.attitudes_count
            
            resweetButton.setTitle(statusViewModel?.retweetStr, forState: .Normal)
            commentButton.setTitle(statusViewModel?.commentStr, forState: .Normal)
            unlikeButton.setTitle(statusViewModel?.unlikeStr, forState: .Normal)
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUPUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置 UI
    private func setUPUI() {
        //添加界面
        addSubview(resweetButton)
        addSubview(commentButton)
        addSubview(unlikeButton)
        
        //设置坐标
        resweetButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton)
        }
        commentButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(resweetButton.snp_trailing)
            make.width.equalTo(unlikeButton)
        }
        unlikeButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(commentButton.snp_trailing)
            make.trailing.equalTo(self)
        }
        
        //添加线的坐标
        let firstLine = addChildLine()
        let secondLine = addChildLine()
        
        //设置图片约束
        firstLine.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(resweetButton.snp_trailing)
            make.centerY.equalTo(self.snp_centerY)
        }
        secondLine.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton.snp_trailing)
            make.centerY.equalTo(self.snp_centerY)
        }
    }
    
    
    //创建按钮的共同方法   创建了一个 Button 名字是 addChildButton( : ) 有两个参数
    func addChildButton(imageName: String, title: String) -> UIButton {
        
        let button = UIButton()
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        //取消高亮
        button.adjustsImageWhenHighlighted = false
        
        //设定文字
        button.setTitle(title, forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(12)
        
        //指定文字颜色
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        //添加按钮
        addSubview(button)
        
        return button
    }
    
    //创建中间的竖线
    private func addChildLine() -> UIImageView {
    
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        
        addSubview(imageView)
        return imageView
    }
}
